package cn.jobstart.common;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 公式
 *
 * @author sunke
 * @version 1.0
 * @date 2021-12-30 23:10
 */
public final class MathFormula {


    private static final String SQRT = "sqrt";//平方根
    private static final String POW = "pow";//次幂
    private static final String PI = "pi";//圆周率
    //private static final String SQUARE = "square";//平方
    private static final String MIN = "min";//最小值
    private static final String MAX = "max";//最大值
    private static final String LOG = "log";//
    private static final String LN = "ln";//
    static ScriptEngine JAVA_MATH = new ScriptEngineManager().getEngineByName("js");

    public static void main(String[] args) {

        //  System.out.println(Math.log10(10));

        String log = "log(A,B)";
       // log = log.replaceAll(",", ")/log(");

        System.out.println(log);

        String formula = log;


        //   String formula1 = "A*B+C+sqrt(DD)-pow(E,F)";

        //  formula = "A+B+C+DD+D+E";
        //   formula = "min(A,B,C)";


        Map<String, String> dataMap = new HashMap<>();

        dataMap.put("A", 10 + "");
        dataMap.put("B", 2 + "");

        System.out.println(compute(formula, dataMap, 10));


     /*   dataMap.put("B", 2 + "");
        dataMap.put("C", 4 + "");*/


        // System.out.println(compute(formula, dataMap, 1));


     /*   Map<String, String> formulaMap = new HashMap<>();

        formulaMap.put("aa", formula);
       // formulaMap.put("bb", formula1);

        System.out.println(computes(formulaMap, dataMap, 1));*/


    }

    /**
     * @param formulaMap 结果集
     * @param dataMap    key 公式  value 对应的值
     * @param digits     有效位数
     * @return
     */
    public static Map<String, BigDecimal> computes(Map<String, String> formulaMap, Map<String, String> dataMap, int digits) {

        Map<String, BigDecimal> resultMap = new HashMap<>();
        for (Map.Entry<String, String> item : formulaMap.entrySet()) {


            BigDecimal result = compute(item.getValue(), dataMap, digits);

            resultMap.put(item.getKey(), result);


        }


        return resultMap;


    }

    /**
     * @param formula 公式
     * @param dataMap 填充的数据
     * @param digits  保留位数
     * @return
     */
    public static BigDecimal compute(String formula, Map<String, String> dataMap, int digits) {

        try {






            //把最长位数的排最前面
            dataMap = MapSortUtil.sortByKeyLengthDesc(dataMap);

            for (Map.Entry<String, String> entry : dataMap.entrySet()) {
                //公式编号
                String key = entry.getKey();
                //有效值
                String value = entry.getValue();
                if (StringUtil.isNotObjEmpty(value)) {
                    formula = formula.replaceAll(key, value);
                }


            }

            formula = formula.replaceAll(SQRT, "Math.sqrt");
            formula = formula.replaceAll(POW, "Math.pow");
            formula = formula.replaceAll(PI, "3.1415926");
            formula = formula.replaceAll(MIN, "Math.min");
            formula = formula.replaceAll(MAX, "Math.max");
            formula = formula.replaceAll(LOG, "baselog");

            formula = formula.replaceAll(LN, "Math.log");



            //添加任意底的的对数
            JAVA_MATH.eval("function baselog(a,b){\n" +
                    "\t\n" +
                    "\treturn Math.log(a)/Math.log(b)\n" +
                    "}");



            return new BigDecimal(JAVA_MATH.eval(formula).toString()).setScale(digits, 4);
        } catch (ScriptException e) {
            e.printStackTrace();

        }


        return null;

    }

    /**
     * @param x 底
     * @param y 数值
     * @return
     */
    public double log(double y, double x) {
        return Math.log(y) / Math.log(x);
    }


}



